package com.egg.ylt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.adapter.ADA_CouponList;
import com.egg.ylt.pojo.CouponEntity;
import com.egg.ylt.presenter.impl.CouponsPresenterImpl;
import com.egg.ylt.view.ICouponsView;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;
import com.yonyou.framework.library.common.utils.ListUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_Coupons extends BaseActivity<CouponsPresenterImpl> implements ICouponsView {

    @BindView(R.id.include_tv_title)
    TextView includeTvTitle;
    @BindView(R.id.include_rl_view)
    RelativeLayout includeRlView;
    @BindView(R.id.coupon_rlv)
    RecyclerView mCouponRlv;
    @BindView(R.id.target_view_ll)
    LinearLayout llTargetView;

    private AppSharedPreferences mSp;
    private String mToken;
    private String mUserId;
    private ADA_CouponList mCouponAdapter;

    private boolean mPayActivityEnter;
    private String mTotalPrice;
    private String mApplyType;
    private String mShopId;
    private String mCardId;

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_coupons;
    }

    @Override
    protected View getLoadingTargetView() {
        return llTargetView;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, includeRlView);


        mPayActivityEnter = getIntent().getBooleanExtra("payActivityEnter", false);
        mTotalPrice = getIntent().getStringExtra("totalPrice");
        mApplyType = getIntent().getStringExtra("applyType");
        mShopId = getIntent().getStringExtra("shopId");
        mCardId = getIntent().getStringExtra("cardId");

        //设置title
        includeTvTitle.setText("优惠券");

        //获取本地用户数据
        mSp = new AppSharedPreferences(mContext);
        mToken = mSp.getString(Constants.USER_TOKEN);
        mUserId = mSp.getString(Constants.USER_ID);
        //初始化recyclerView
        initRecycler();
    }

    private void initRecycler() {
        //设置布局管理器
        mCouponRlv.setLayoutManager(new LinearLayoutManager(mContext));
        //设置适配器
        mCouponAdapter = new ADA_CouponList(mTotalPrice);
        mCouponRlv.setAdapter(mCouponAdapter);

        if (mPayActivityEnter) {
            mCouponAdapter.setOnItemClickListener(new ADA_CouponList.OnItemClickListener() {
                @Override
                public void onItemClicked(String id, String ticketId, String price) {
                    Intent intent = new Intent();
                    intent.putExtra("id", id);
                    intent.putExtra("ticketId", ticketId);
                    intent.putExtra("ticketAmount", price);
                    setResult(1, intent);
                    finish();
                }
            });
        }
        //请求用户优惠卷数据
        mPresenter.getCouponList(mToken, Constants.COMPANYID, mUserId, mApplyType, mShopId, mCardId);
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    /**
     * 获取用户优惠卷集合数据
     *
     * @param entity
     */
    @Override
    public void getCouponList(CouponEntity entity) {

        if (ListUtil.isListEmpty(entity.getList())) {
            toggleShowEmpty(true, R.drawable.empty_no_response, "暂无优惠券");
            return;
        }

        toggleShowEmpty(false, -1, "");

        // 设置数据
        mCouponAdapter.setList(entity.getList());
    }

    @OnClick({R.id.include_iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.include_iv_back://返回键
                finish();
                break;
        }
    }

}
